package com.liu.cloud.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.PathContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author LIU
 * @version 1.0
 * @description: 全局过滤器
 * @date 2023/7/29 17:51
 */
@Slf4j
@Component
public class MyGloBalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 请求路径
        String path = exchange.getRequest().getPath().value();

        // 请求开始时间
        long start = System.currentTimeMillis();
        return chain.filter(exchange)
                .then(Mono.fromRunnable(() -> log.info("spend time: {} , path: {} ", System.currentTimeMillis() - start, path )));
    }
}
